<template>
	<view class="page">
		<view class="header">
			<view class="left" @click="openActivity">
				<image class="image" src="../../../static/me/integral/my_record.png"></image>
				<view>关闭活动</view>
			</view>
			<view class="right">
				<view class="image">{{ volunteer_time }}</view>
				<view>志愿时长</view>
			</view>
		</view>

		<view class="aside">
			<view class="section" v-for="(item, i) in list" :key="i">
				<view class="left"><span>{{item.count}}</span>分钟</view>
				<view class="right">
					<view>开始：{{item.start_time}}</view>
					<view>结束：{{item.end_time}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getVolunteerList,
		getMyVolunteerTime,
		endLocation
	} from '../../../network/me.js'
	export default {
		data() {
			return {
				isOpen: false,
				volunteer_time: 0,
				list: []
			};
		},
		onLoad() {
			this.isOpen = !!this.$store.state.userInfo.location_status
			this.init()
			this.time()
		},
		onShow() {
			this.isOpen = !!this.$store.state.userInfo.location_status
		},
		methods: {
			async time() {
				const result = await getMyVolunteerTime('家属1')
				if (!result.flag) return
				this.volunteer_time = (+result.data[0].volunteer_time).toFixed(1)
			},

			async init() {
				const result = await getVolunteerList()

				// console.log(result)
				if (!result.flag) return
				this.list = result.data
					.filter(item => item.start_time && item.end_time)
					.reverse()
					.map(item => {
						item.count = this.countTime(item.start_time, item.end_time);
						return item
					})
			},

			countTime(start, end) {
				let time = new Date(end).getTime() - new Date(start).getTime()
				return Math.round(time / 60000)
			},

			async openActivity() {
				if (this.isOpen) {
					// 关闭
					const result = await endLocation()
					console.log(result)
					if (!result.flag) {
						uni.showToast({
							title: result.result,
							icon: 'none'
						})
						return 
					}
					this.isOpen = !this.isOpen;
					this.init()
					this.time()
					uni.setStorageSync('activity', this.isOpen)
					this.$store.commit('setLocationStatus', 0)
					return
				}
				uni.showToast({
					title: '当前没有活动进行',
					icon: 'none'
				})
			}
		}
	}
</script>

<style lang="scss">
	.page {
		.header {
			display: flex;
			justify-content: space-around;
			margin: 10px;
			padding: 15px 0;
			background-color: #1CBAB6;
			border-radius: 5px;
			color: white;

			&>view {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;

				&:nth-of-type(1) {
					border-right: 0.5px dotted #F2F2F2;
				}

				&>.image {
					width: 60px;
					height: 60px;
					line-height: 60px;
					font-size: 20px;
					text-align: center;
					background-color: white;
					border-radius: 50%;
					margin-bottom: 10px;
					color: rgb(159, 198, 255);
				}
			}
		}

		.aside {
			margin: 10px;

			.section {
				display: flex;
				background-color: white;
				padding: 10px 0;
				align-items: center;
				border-radius: 2px;
				margin: 8px 0;
				box-shadow: 0 0 2px #CCCCCC;

				.left {
					width: 70px;
					color: #1CBAB6;
					text-align: center;
					border-right: 1px dotted #F2F2F2;
					font-size: 14px;

					span {
						font-size: 18px;
					}
				}

				.right {
					flex: 1;
					padding-left: 15px;
				}
			}
		}

	}
</style>
